package com.tenebrous.jestic.entity.framwork.meber.organ;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Slf4j
@Data
@Table(name = "ORG_USER")
public class OrgUser implements Serializable {

    private static final long serialVersionUID = -1450339698390393194L;

    @Id
    private String id;

    private String employsn;

    private String realname;
    private String oldrealname;

    private String sexCode;
    private String sexName;

    private String birthday;

    private String nationCode;
    private String nationName;

    private String nativeCode;
    private String nativeName;

    private String birthaddr;

    private String politiCode;
    private String politiName;

    private String comunistime;
    private String worktime;

    private String edutionCode;
    private String edutionName;
    private String edutionSchool;
    private String edutionMajor;
    private String edutionGradutime;

    private String degreeCode;
    private String degreeName;

    private String titleleve;

    private String marryCode;
    private String marryName;

    private String healthyCode;
    private String healthyName;
    private String healthyDesc;

    private String enleveCode;
    private String enleveName;

    private String pcleveCode;
    private String pcleveName;

    private String address;
    private String idcard;
    private String email;
    private String phone;
    private String phonecode;
    private String mobile;
    private String mobilebelong;
    /**
     * 职级ID
     */
    private String jobleve;
    private String joblevetime;

    private String contractor;
    private String contractship;
    private String contractphone;

    private String technicPosit;
    private String technicSpecial;

    private String creditReport;
    private String evaluate;

    private String islegal;

    private String avatar;

    private String entrytime;

    private String fomaltime;

    private String leavetime;

    private String retiretime;

    private String deathtime;

    private Integer dstate;

    private String estate;

    private String etype;

    private String remark;

    private String tags;

    private Long sort;
    /**
     * 岗位ID
     */
    private String stationid;
    /**
     * 档级
     */
    private String stationgradid;

    private String departmentid;

    private String procinstid;

    private Date createDate;

    private String createUserid;

    private Date updateDate;

    private String updateUserid;

    /******************************扩展属性*****************************/

    @Transient
    private String defultIndexs;
    /**
     * 登录账号、用户名
     */
    @Transient
    private String username;
    /**
     * 单位ID
     */
    @Transient
    private String companyid;
    /**
     * 单位名称
     */
    @Transient
    private String companyname;
    /**
     * 部门名称
     */
    @Transient
    private String departmentname;
    /**
     * 角色ID
     */
    @Transient
    private String roleids;
    /**
     * 角色编码
     */
    @Transient
    private String rolecodes;
    /**
     * 角色名称
     */
    @Transient
    private String rolenames;
    /**
     * 层级代码
     */
    @Transient
    private String leveid;
    /**
     * 层级代码
     */
    @Transient
    private String levecode;
    /**
     * 层级名称
     */
    @Transient
    private String levename;
    /**
     * 序列代码
     */
    @Transient
    private String sequnceid;
    /**
     * 序列代码
     */
    @Transient
    private String sequncecode;
    /**
     * 序列名称
     */
    @Transient
    private String sequncename;
    /**
     * 岗位代码
     */
    @Transient
    private String stationcode;
    /**
     * 岗位名称
     */
    @Transient
    private String stationname;
    /**
     * 薪酬档级
     */
    @Transient
    private String stationgradname;

    @Transient
    private List<OrgUserWork> worklist = new ArrayList<>();
    @Transient
    private List<OrgUserCerti> certilist = new ArrayList<>();
    @Transient
    private List<OrgUserEducate> educatelist = new ArrayList<>();
    @Transient
    private List<OrgUserTrain> trainlist = new ArrayList<>();
    @Transient
    private List<OrgUserFamily> familylist = new ArrayList<>();

    /**
     * g根據出生年月计算年龄
     *
     * @return
     */
    public Integer getAge() {
        String birthdaystr = this.getBirthday();
        if (birthdaystr != null && birthdaystr.trim().length() > 0) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM");
            try {
                Calendar now = Calendar.getInstance();
                Calendar cal = Calendar.getInstance();
                cal.setTime(sdf.parse(birthdaystr.trim()));
                if (cal.before(now)) { //出生日期晚于当前时间，无法计算
                    int yearNow = now.get(Calendar.YEAR);  //当前年份
                    int monthNow = now.get(Calendar.MONTH);  //当前月份

                    int yearBirth = cal.get(Calendar.YEAR);
                    int monthBirth = cal.get(Calendar.MONTH);

                    int age = yearNow - yearBirth;   //计算整岁数
                    if (monthNow < monthBirth) {
                        age--;//当前月份在生日之前，年龄减一
                    }
                    return age;
                }
            } catch (ParseException e) {
                log.error("计算年齡错误", e);
            }
        }
        return null;
    }

    public String getStationtext() {
        return String.format("%s - （%s，%s）", this.getStationname() == null ? "" : this.getStationname(), (this.getSequncecode() == null ? "" : this.getSequncecode()) + (this.getSequncename() == null ? "" : this.getSequncename()), (this.getLevecode() == null ? "" : this.getLevecode()) + (this.getLevename() == null ? "" : this.getLevename()));
    }
}